﻿//====================================================================
// Create by Daniel
// file： YCUser.cs
// project ：YunCMS
// Build time：2012-09-06
// modify:
// description:
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using YunCMS.Model;
using YunCMS.IDAL;
using YunCMS.Common;

namespace YunCMS.BLL
{
    /// <summary>
    /// Business Logic Layer For YCUser.
    /// </summary>
    public class YCUserBLL
    {

		///<summary>
		/// DAL Instance.
		///</summary>
        public static readonly IYCUserDAL dal = DALFactory.CreateYCUser();
		

		/// <summary>
		/// constructor
		/// </summary>
		public YCUserBLL()
		{
		}

		#region Insert update delete
		/// <summary>
		/// insert a YCUser
		/// </summary>
		/// <param name="model">YCUser</param>
		/// <returns>The id of the new item</returns>
		public int Add(YCUser model)
		{
			// Validate input
			if (model == null)
				return 0;
			// Use the dal to insert a new record 
			return dal.Add(model);
		}

		/// <summary>
		/// update a YCUser.
		/// </summary>
		/// <param name="model">YCUser</param>
		/// <returns>Rows affected</returns>
		public int Update(YCUser model)
		{
            // Validate input
			if (model==null)
				return 0;
			// Use the dal to update a new record 
			return dal.Update(model);
		}
				
		/// <summary>
		/// delete a item
		/// </summary>
	    /// <param name="model">id</param>
		/// <returns>affectes rows</returns>
		public int Remove(int id)
		{
			// Validate input
			if(id<0)
				return 0;
			return dal.Delete(id);
		}
				
        /// <summary>
		/// Get a YCUser 
		/// </summary>
		/// <param name="ID">id</param>
		/// <returns>YCUser entity</returns>
		public YCUser GetYCUser(int id)
		{
			// Validate input
			if(id<0)
				return null;

			// Use the dal to get a record 
			return dal.GetYCUser(id);
		}

        public YCUser GetYCUser(string userAccount)
        {
            // Validate input
            if (string.IsNullOrEmpty(userAccount))
                return null;

            // Use the dal to get a record 
            return dal.GetYCUser(userAccount);

        }
		/// <summary>
		/// get YCUsers
		/// </summary>
		/// <returns>List</returns>
		public IList<YCUser> GetList(string account)
		{
			// Use the dal to get all records 
            if (account == null)
            {
                return dal.GetList(string.Empty);
            }
            return dal.GetList(account);
		}

        public int UpdateDeactive(int id, bool value)
        {
            return dal.UpdateOneField(id, "Deactive", value);
        }			
		
		/// <summary>
        /// if IsExist a item accroding to id
        /// </summary>
        /// <param name="ID">id</param>
        /// <returns>true/false</returns>
        public bool IsExist(string userAccount, string password)
		{
            if (string.IsNullOrEmpty(userAccount) || string.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("name,password", "can't be empty!");
            }
            //encode password
            return dal.IsExist(userAccount.ToLower(), YunCMSUtil.EncryptPwd(password));
		}

        public bool IsExist(int id)
        {
            return dal.IsExist(id);
        }
        #endregion	
		
    }
}






